package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.acticle.domain.Destination;
import cn.wolfcode.luowowo.acticle.service.IDestinationService;
import cn.wolfcode.luowowo.hotel.domain.Hotel;
import cn.wolfcode.luowowo.hotel.domain.HotelTheme;
import cn.wolfcode.luowowo.hotel.query.HotelQueryObject;
import cn.wolfcode.luowowo.hotel.service.IAreaService;
import cn.wolfcode.luowowo.hotel.service.IHotelService;
import cn.wolfcode.luowowo.hotel.service.IHotelThemeService;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IDestinationSearchService;
import cn.wolfcode.luowowo.search.service.ISearchService;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("hotel")
public class HotelController {
    @Reference
    private IHotelThemeService hotelThemeService;
    @Reference
    private IHotelService hotelService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private ISearchService searchService;
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private IAreaService areaService;

    @RequestMapping("")
    public String index(Model model) {
        List<HotelTheme> themes = hotelThemeService.list();
        Collections.shuffle(themes);//随机顺序
        if (themes.size() > 6) {
            themes = themes.subList(0, 6);//展示6个主题
        }
        model.addAttribute("themes", themes);
        model.addAttribute("favDests", hotelService.list6DestsByFavorHotel());

        return "hotel/index";
    }

    @RequestMapping("{destId}")
    public String list(Model model, @ModelAttribute("destId")@PathVariable Long destId) {
        Destination country = destinationService.getCountry(destId);
        if ("中国".equals(country.getName())) {
            country.setChildren(destinationService.getHotStartPlacesInChina().subList(0,5));
        }
        model.addAttribute("country", destinationService.getCountry(destId));
        model.addAttribute("dest", destinationSearchService.get(destId));
        model.addAttribute("themes", hotelThemeService.queryByDestId(destId));
        model.addAttribute("areas", areaService.queryByDestId(destId));
        return "hotel/list";
    }

    @RequestMapping("queryhotels")
    public String queryhotels(Model model,  @ModelAttribute("qo")HotelQueryObject qo) {

        model.addAttribute("pageInfo", hotelService.query(qo));
        return "hotel/ajaxTpl";
    }

    /**
     * 列出特价酒店
     */
    @RequestMapping("favHotels")
    public String favHotels(Model model, HotelQueryObject qo) {
        qo.setPageSize(8);
        qo.setState(Hotel.STATE_FAVOR);
        model.addAttribute("favHotels", hotelService.query(qo).getList());
        return "hotel/favhotelTpl";
    }
    /**
     * index页面搜索
     */
    @RequestMapping("q")
    private String hotelSearch(Model model, @ModelAttribute("qo")SearchQueryObject qo) {
        Page<DestinationTemplate> dests = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME
                , DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo, "name");

        SearchResultVO vo = new SearchResultVO();
        vo.setDests(dests.getContent());
        model.addAttribute("data", vo);
        return "hotel/searchHotel";
    }
    /**
     * list页面搜索
     */
    @RequestMapping("s.php")
    private String hotelSearch2(Model model, SearchQueryObject qo) {
        //data.dest
        Page<DestinationTemplate> dests = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME
                , DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo, "name");

        return list(model,dests.getContent().get(0).getId());
    }

    //防报错
    @RequestMapping("ajax.php")
    @ResponseBody
    public Object Ajax() {
        return JSON.parseObject("{data:{}}");
    }
}
